package com.linxi.wordslog.wordslog

import android.app.Dialog
import android.os.Bundle
import android.view.*
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.Navigation
import com.linxi.wordslog.Injection
import com.linxi.wordslog.R
import com.linxi.wordslog.wordslogviewmodel.WordViewModel
import kotlinx.android.synthetic.main.fragment_user_info.*
import kotlinx.android.synthetic.main.user_info_cell.*


// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [UserInfoFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class UserInfoFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    private var saveSex = -1
    private val wordsViewModel by activityViewModels<WordViewModel> {
        Injection.provideWordViewModelFactory(requireActivity())
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user_info, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        wordsViewModel.apply {
            nameEdit.setText(name.value)
            ageEdit.setText(age.value.toString())
            when (sex.value) {
                1 -> {
                    sexEdit.text = "男"
                }
                0 -> {
                    sexEdit.text = "女"
                }
                else -> {
                    sexEdit.text = "保密"
                }
            }
            signEdit.setText(sign.value)
        }
        saveButton.setOnClickListener {
            setOf(nameEdit, ageEdit, signEdit).forEach { edit ->
                edit.clearFocus()
            }
            val saveName = nameEdit.text.toString()
            if (saveName.isEmpty()) {
                Toast.makeText(this.context, "姓名不可为空", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    val saveAge = ageEdit.text.toString().toInt()
                    val saveSign = signEdit.text.toString()
                    wordsViewModel.saveInfo(saveName, saveAge, saveSex, saveSign)
                    Navigation.findNavController(it).navigateUp()
                } catch (e: Exception) {
                    Toast.makeText(this.context, "信息不可为空", Toast.LENGTH_SHORT).show();
                }
            }
        }
//        nameLayout.setOnClickListener {
//            nameEdit.requestFocus()
//        }
        sexLayout.setOnClickListener {
            showBottomDialog()
        }


    }

    private fun showBottomDialog() {
        //1、使用Dialog、设置style
        val activity = requireActivity()
        val dialog = Dialog(activity, R.style.DialogTheme)
        //2、设置布局
        val view = View.inflate(activity, R.layout.dialog, null)
        dialog.setContentView(view)
        val window: Window? = dialog.window
        //设置弹出位置
        if (window != null) {
            window.setGravity(Gravity.BOTTOM)
            window.setWindowAnimations(R.style.main_menu_animStyle)
            //设置对话框大小
            window.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        }
        //设置弹出动画
        dialog.show()
        dialog.findViewById<TextView>(R.id.select_secrecy).setOnClickListener {
            sexEdit.text = "保密"
            saveSex = -1
            dialog.dismiss()
        }
        dialog.findViewById<TextView>(R.id.select_male).setOnClickListener {
            sexEdit.text = "男"
            saveSex = 1
            dialog.dismiss()
        }
        dialog.findViewById<TextView>(R.id.select_female).setOnClickListener {
            sexEdit.text = "女"
            saveSex = 0
            dialog.dismiss()
        }
        dialog.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
            dialog.dismiss()
        }
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment UserInfoFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            UserInfoFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}